home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / faq / wdj0597.zip / ROBBINS.ZIP / PEIMAGE.CPP < prev    next >
C/C++ Source or Header  |  1997-02-11  |  4KB  |  124 lines

  1. /*//////////////////////////////////////////////////////////////////
  2.                        The Main Includes
  3. //////////////////////////////////////////////////////////////////*/
  4. // Why won't the windows header compile with warning level 4?
  5. #pragma warning ( disable : 4201 )
  6. #define WIN32_LEAN_AND_MEAN
  7. #include <windows.h>
  8. #include <imagehlp.h>
  9. #include <stdlib.h>
  10. #include <tchar.h>
  11. #include <direct.h>
  12. #include "PEImage.h"
  13.  
  14. /*//////////////////////////////////////////////////////////////////
  15.                   Public constructors and destructors.
  16. //////////////////////////////////////////////////////////////////*/
  17. CPEImage :: CPEImage ( void )
  18. {
  19.     // Initialize all class members to a known state.
  20.     memset ( &m_stLoadedImage , NULL , sizeof ( LOADED_IMAGE ) ) ;
  21. }
  22.  
  23. CPEImage :: ~CPEImage ( void )
  24. {
  25.     // Unload if necessary.
  26.     UnloadImage ( ) ;
  27. }
  28.  
  29. /*//////////////////////////////////////////////////////////////////
  30.            Public Initialization and Cleanup Member Functions
  31. //////////////////////////////////////////////////////////////////*/
  32. PEI_ERROR CPEImage :: LoadImage ( LPCTSTR szFile )
  33. {
  34.     if ( NULL == szFile )
  35.     {
  36.         return ( PEI_BADPARAMS ) ;
  37.     }
  38.  
  39.     if ( NULL != m_stLoadedImage.ModuleName )
  40.     {
  41.         return ( PEI_ALREADYINIT ) ;
  42.     }
  43.  
  44.     // OK, the MapAndLoad API searches all over the path for a
  45.     //  file to load.  I want mine to be concentrate on just
  46.     //  the file passed in, therefore, I make sure that there
  47.     //  is some sort of path characters in the string, such
  48.     //  as ".." or "\".  If there is none, then I will just
  49.     //  build the filename with the complete current directory.
  50.     // A temporary buffer where we stick the filename.
  51.     TCHAR szRealFileName [ MAX_PATH ] ;
  52.     _tcscpy ( szRealFileName , szFile ) ;
  53.     if ( NULL == _tcspbrk ( szRealFileName , _T ( "\\" ) ) )
  54.     {
  55.         // The file is just a filename.  Put the current
  56.         //  directory on it.
  57.         _tgetcwd ( szRealFileName , sizeof ( szRealFileName ) ) ;
  58.         _tcscat ( szRealFileName , _T ( "\\" ) ) ;
  59.         _tcscat ( szRealFileName , szFile ) ;
  60.     }
  61.  
  62.     // Now we are ready to load up the image.
  63.     BOOL bRet = ::MapAndLoad ( szRealFileName   ,
  64.                                NULL             ,
  65.                                &m_stLoadedImage ,
  66.                                TRUE             ,
  67.                                TRUE              ) ;
  68.     // Skip out now if we need to.
  69.     if ( FALSE == bRet )
  70.     {
  71.         return ( PEI_NOTMAPPED ) ;
  72.     }
  73.     return ( PEI_NOERROR ) ;
  74. }
  75.  
  76. void CPEImage :: UnloadImage ( void )
  77. {
  78.     // Is there anything that we need to unload?
  79.     if ( NULL != m_stLoadedImage.ModuleName )
  80.     {
  81.         ::UnMapAndLoad ( &m_stLoadedImage ) ;
  82.         // Reinitialize the main structure so this class instance
  83.         //  could be used again.
  84.         memset ( &m_stLoadedImage , NULL , sizeof ( LOADED_IMAGE));
  85.     }
  86. }
  87.  
  88. const PIMAGE_SECTION_HEADER CPEImage
  89.                          :: GetSectionHeader ( int iSection ) const
  90. {
  91.     if ( iSection >= m_stLoadedImage.FileHeader->
  92.                                       FileHeader.NumberOfSections )
  93.     {
  94.         return ( NULL ) ;
  95.     }
  96.  
  97.     return ( &m_stLoadedImage.LastRvaSection[ iSection ] ) ;
  98. }
  99.  
  100. DWORD CPEImage :: GetDataDirectoryRVA ( int iDataDir ) const
  101. {
  102.     if ( iDataDir >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES )
  103.     {
  104.         return ( (DWORD)-1 ) ;
  105.     }
  106.     return ( m_stLoadedImage.FileHeader->
  107.            OptionalHeader.DataDirectory[ iDataDir ].VirtualAddress);
  108. }
  109.  
  110.  
  111. LPCVOID CPEImage :: ImageRvaToFileVa ( DWORD dwRVA ) const
  112. {
  113.     return ((LPCVOID)::ImageRvaToVa( m_stLoadedImage.FileHeader    ,
  114.                                      m_stLoadedImage.MappedAddress ,
  115.                                      dwRVA                         ,
  116.                                      NULL                        ));
  117. }
  118.  
  119. const PIMAGE_NT_HEADERS CPEImage :: GetImageHeaders ( void ) const
  120. {
  121.     return ( m_stLoadedImage.FileHeader ) ;
  122. }
  123.  
  124.